using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace VideoSource
{
    public partial class VideoSourceUC : UserControl
    {
        private VideoSource videoSource;

        public VideoSource VideoSource
        {
            get { return videoSource; }
            set
            {
                videoSource = value;
            }
        }

        public VideoSourceUC()
        {
            InitializeComponent();
            this.videoSource = new VideoSource();
        }


        private void btnStart_Click(object sender, EventArgs e)
        {
            videoSource.Start();
            GetFPS();
            GetSize();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            videoSource.Stop();
        }

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            videoSource.ShowVideoSource();
        }

        private void btnSelectFormat_Click(object sender, EventArgs e)
        {
            videoSource.ShowVideoFormat();
        }

        private void btnSetFPS_Click(object sender, EventArgs e)
        {
            try
            {
                if (!videoSource.SetFramesPerSec(Double.Parse(txtFPS.Text)))
                {
                    MessageBox.Show("Couldn't set FPS!");
                }
                GetFPS();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Couldn't set FPS: " + ex.ToString());
            }
        }

        private void GetFPS()
        {
            try
            {
                txtFPS.Text = string.Format("{0:f}", videoSource.GetFramesPerSec());
            }
            catch
            {
                txtFPS.Text = "";
            }
        }

        private void btnSetSize_Click(object sender, EventArgs e)
        {
            try
            {
                string[] tokens = txtSize.Text.Trim().Split(new char[] { 'x', 'X' });
                if (!videoSource.SetFrameSize(Convert.ToInt32(tokens[0]), Convert.ToInt32(tokens[1])))
                {
                    MessageBox.Show("Couldn't set size!");
                }
                GetFPS();
                GetSize();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Couldn't set size: " + ex.ToString());
            }

        }

        private void GetSize()
        {
            try
            {
                Size size = videoSource.GetFrameSize();
                txtSize.Text = string.Format("{0}x{1}", size.Width, size.Height);
            }
            catch
            {
                txtSize.Text = "";
            }

        }
    }
}
